package gunfire.relics;

import basemod.abstracts.CustomRelic;
import basemod.abstracts.CustomSavable;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.MathHelper;
import com.megacrit.cardcrawl.helpers.PowerTip;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import gunfire.GunfireMod;
import gunfire.util.TextureLoader;

import static gunfire.GunfireMod.makeRelicOutlinePath;
import static gunfire.GunfireMod.makeRelicPath;

public class SoulOfTaizi extends CustomRelic implements CustomSavable<int[]> {

    public static final String ID = GunfireMod.makeID("SoulOfTaizi");
    private static final Texture IMG = TextureLoader.getTexture(makeRelicPath("SoulOfTaizi.png"));
    private static final Texture OUTLINE = TextureLoader.getTexture(makeRelicOutlinePath("blank.png"));
    public static final int P1 = 20; //元素伤害牌
    public static final int P2 = 20; //法球牌
    public static final int P3 = 120; //给予敌人护甲
    public int progress1;
    public int progress2;
    public int progress3;
    public float offsetX = 0;

    public SoulOfTaizi() {
        super(ID, IMG, OUTLINE, RelicTier.STARTER, LandingSound.CLINK);
        this.progress1 = 0;
        this.progress2 = 0;
        this.progress3 = 0;
    }

    @Override
    public String getUpdatedDescription() {
        return DESCRIPTIONS[0];
    }

    @Override
    public void onEquip() {
        this.refreshDesc();
    }

    @Override
    public void onPlayCard(AbstractCard c, AbstractMonster m) {
        if(c.hasTag(GunfireMod.ELEMENT_DAMAGE_CARD)){
            this.progress1++;
            this.flash();
        }
        if(c.hasTag(GunfireMod.ENERGY_ORB_CARD)){
            this.progress2++;
            this.flash();
        }
        this.judgeEvolve();
    }

    private void refreshDesc(){
        this.description = DESCRIPTIONS[1] + P1 + DESCRIPTIONS[2] + this.progress1 + " / " + P1 +
                DESCRIPTIONS[3] + P2 + DESCRIPTIONS[4] + this.progress2 + " / " + P2 +
                DESCRIPTIONS[5] + P3 + DESCRIPTIONS[6] + this.progress3 + " / " + P3;
        this.tips.clear();
        this.tips.add(new PowerTip(this.name, this.description));
        this.initializeTips();
    }

    private void getOffsetX(){
        float target = (float)(-relicPage * Settings.WIDTH) + (float)relicPage * (PAD_X + 36.0F * Settings.xScale);
        if (AbstractDungeon.player.relics.size() >= MAX_RELICS_PER_PAGE + 1) {
            target += 36.0F * Settings.scale;
        }
        if (this.offsetX != target) {
            this.offsetX = MathHelper.uiLerpSnap(offsetX, target);
        }
    }

    @Override
    public void renderCounter(SpriteBatch sb, boolean inTopPanel) {
        this.getOffsetX();
        if (inTopPanel) {
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress1 + "/" + P1, this.offsetX + this.currentX + 30.0F * Settings.scale, this.currentY - 20.0F * Settings.scale, Color.WHITE);
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress2 + "/" + P2, this.offsetX + this.currentX + 30.0F * Settings.scale, this.currentY - 40.0F * Settings.scale, Color.WHITE);
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress3 + "/" + P3, this.offsetX + this.currentX + 30.0F * Settings.scale, this.currentY - 60.0F * Settings.scale, Color.WHITE);
        } else {
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress1 + "/" + P1, this.currentX + 30.0F * Settings.scale, this.currentY - 20.0F * Settings.scale, Color.WHITE);
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress2 + "/" + P2, this.currentX + 30.0F * Settings.scale, this.currentY - 40.0F * Settings.scale, Color.WHITE);
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress3 + "/" + P3, this.currentX + 30.0F * Settings.scale, this.currentY - 60.0F * Settings.scale, Color.WHITE);
        }
    }

    public void judgeEvolve(){
        if(this.progress1 >= P1){
            new SoulOfElement().obtain();
        } else if(this.progress2 >= P2){
            new SoulOfOrb().obtain();
        } else if(this.progress3 >= P3){
            new SoulOfIron().obtain();
        } else {
            this.refreshDesc();
        }
    }

    @Override
    public AbstractRelic makeCopy() {
        return new SoulOfTaizi();
    }

    @Override
    public int[] onSave() {
        return new int[]{this.progress1, this.progress2, this.progress3};
    }

    @Override
    public void onLoad(int[] p) {
        this.progress1 = p[0];
        this.progress2 = p[1];
        this.progress3 = p[2];
        this.refreshDesc();
    }
}
